package com.xingshang.service.impl;

import cn.hutool.core.lang.hash.Hash;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingshang.pojo.RecentPlayed;
import com.xingshang.service.RecentPlayedService;
import com.xingshang.mapper.RecentPlayedMapper;
import com.xingshang.utils.JwtHelper;
import com.xingshang.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 87547
* @description 针对表【recent_played】的数据库操作Service实现
* @createDate 2024-12-13 22:22:01
*/
@Service
public class RecentPlayedServiceImpl extends ServiceImpl<RecentPlayedMapper, RecentPlayed>
    implements RecentPlayedService{

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private RecentPlayedMapper recentPlayedMapper;

    /*
    响应数据：
{
  code: 200,         // 状态码
  message: string,   // 提示信息
  data: {
    list: [{         // 歌曲列表
      id: string,    // 歌曲ID
      mid: string,   // 歌曲MID
      name: string,  // 歌曲名称
      cover: string, // 封面图片URL
      author: string // 歌手名称
    }],
    hasMore: boolean // 是否还有更多数据
  }
}

     */
    @Override
    public Result getList(String token, Integer pageNum, Integer pageSize) {
        Long userId = jwtHelper.getUserId(token);
        IPage<RecentPlayed> recentPlayedIPage = new Page<>(pageNum, pageSize);
        QueryWrapper<RecentPlayed> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userId);

        recentPlayedMapper.selectPage(recentPlayedIPage, queryWrapper);
        // 是否还有更多数据
        boolean hasMore = recentPlayedIPage.getTotal() > (long) pageNum * pageSize;
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", recentPlayedIPage.getRecords());
        map.put("hasMore", hasMore);
        return Result.ok(map);

    }
}




